import {ActionName, FruitName} from "@/components/type";
import Action from "@/modal/Action";
import {reactive} from "vue";
import Score from "@/modal/Score";
import {ScoreType} from "@/components/ScorePanel/type";
import Goods from "@/modal/Goods";

const score = reactive(Score.getInstance());
const goods = reactive(Goods.getInstance());
const action = reactive(Action.getInstance());
export default class Fruit {
  readonly name: FruitName;
  readonly chineseName: FruitName;
  readonly maxStep: number;
  private currentStage_: number = 0;

  constructor(options?: any) {
    this.name = options?.name;
    this.chineseName = options?.chineseName;
    this.maxStep = options?.maxStep;
  }

  get currentStep() {return this.currentStage_;}

  // 是否已完全成熟
  get isRipen() {return this.currentStep === this.maxStep;}

  // 成熟点位
  get ripenSteps() {return this.currentStep % 1 === 0;}

  // 成熟个数
  get ripenNumber() {
    return this.currentStep;
  }

  growUp(): Fruit {
    if (!this.isRipen) {
      this.currentStage_++;
    }
    return this;
  }

  isHtmlActive(step: number) {return step > 0;}

  getAward() {return this;}

}

// 苹果
export class Apple extends Fruit {
  readonly name = FruitName.APPLE;
  readonly chineseName = FruitName.CHINESE_APPLE;
  readonly maxStep: number = 8;
  readonly score: number = 3;

  override isHtmlActive(step: number) {return step !== 1 && step !== 3;}

  get ripenSteps() {return this.currentStep !== 1 && this.currentStep !== 3;}

  get ripenNumber() {
    return Math.floor(this.currentStep < 5 ? this.currentStep / 2 : this.currentStep - 2);
  }

  getAward() {
    if (this.ripenSteps) {
      score.addScore(this.score, ScoreType.FRUIT);
    }
    return this;
  }
}

// 桃子
export class Peach extends Fruit {
  readonly name = FruitName.PEACH;
  readonly chineseName = FruitName.CHINESE_PEACH;
  readonly maxStep: number = 8;
  readonly action: boolean = true;

  override isHtmlActive(step: number) {return step % 2 === 0;}

  get ripenSteps() {return this.currentStep % 2 === 0;}

  get ripenNumber() {
    return Math.floor(this.currentStep / 2);
  }

  getAward() {
    if (this.ripenSteps) {
      action.setAction([ActionName.WATER]);
    }
    return this;
  }
}

// 黑莓
export class Blackberry extends Fruit {
  readonly name = FruitName.BLACKBERRY;
  readonly chineseName = FruitName.CHINESE_BLACKBERRY;
  readonly maxStep: number = 7;
  readonly score: number = 2;

  getAward() {
    if (this.ripenSteps) {
      score.addScore(this.score, ScoreType.FRUIT);
    }
    return this;
  }
}

// 树莓
export class Raspberry extends Fruit {
  readonly name = FruitName.RASPBERRY;
  readonly chineseName = FruitName.CHINESE_RASPBERRY;
  readonly maxStep: number = 9;
  readonly goodsNum: number = 3;

  public showHtmlScore(step: number): any {return step % 3 === 0 ? Math.floor(step / 3) : undefined;}

  get score(): number {return this.currentStep % 3 === 0 ? Math.floor(this.currentStep / 3) : 0;}

  getAward() {
    if (this.ripenSteps) {
      goods.gainGoods(this.goodsNum);
      if (this.score > 0) score.addScore(this.score, ScoreType.FRUIT);
    }
    return this;
  }
}
